package cn.com.sbell.vss.util;

import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;


public class VssValidator {
    
    /**
     *
     * @param field String
     * @param value String
     */
    public static void validateRequired(String field, String value) {
        if (Util.strIsNull(value)) {
            throw new VssException(VssConstants.CODE_REQUIRED,
                    new String[] {field});
        }
        
    }
    
    /**
     *
     * @param field String
     * @param value String
     * @param required boolean
     * @param dataType int
     */
    public static void validateNumber(String field, String value,
            boolean required, int dataType) {        
        if(Util.strIsNull(value)){
            if(required){
                throw new VssException(VssConstants.CODE_REQUIRED,  new String[] {field});
            }else{
                return;
            }
        }
        validateNumber(field, value, dataType);
    }
    
    /**
     *
     * @param field String
     * @param value String
     * @param required boolean
     * @param maxLength int
     */
    public static void validateMaxLength(String field, String value,
            boolean required,
            int maxLength) {
        if(Util.strIsNull(value)){
            if(required){
                throw new VssException(VssConstants.CODE_REQUIRED,  new String[] {field});
            }else{
                return;
            }
        }
        
        if (maxLength > 0
                && !Util.validateMaxlength(value, maxLength)) {
            throw new VssException(VssConstants.CODE_MAXLENGTH,
                    new String[] {field,
                    String.valueOf(maxLength)});
        }
        
    }
    
    /**
     *
     * @param field String
     * @param value String
     * @param required boolean
     * @param datePattern String
     */
    
    public static void validateDate(String field, String value,
            boolean required,
            String datePattern) {
        if(Util.strIsNull(value)){
            if(required){
                throw new VssException(VssConstants.CODE_REQUIRED,  new String[] {field});
            }else{
                return;
            }
        }
        validateDataTypeOfDate(field, value, datePattern);
    }
    
    /**
     *
     * @param field String
     * @param value String
     * @param dataType int
     */
    public static void validateNumber(String field, String value,
            int dataType) {
        if (dataType == VssConstants.DATA_INTEGER) {
            if (!Util.validateInt(value)) {
                throw new VssException(VssConstants.CODE_INTEGER,
                        new String[] {field});
                
            }
        } else if (dataType == VssConstants.DATA_LONG) {
            if (!Util.validateLong(value)) {
                throw new VssException(VssConstants.CODE_LONG,
                        new String[] {field});
                
            }
        } else if (dataType == VssConstants.DATA_DOUBLE) {
            if (!Util.validateDouble(value)) {
                throw new VssException(VssConstants.CODE_DOUBLE,
                        new String[] {field});
                
            }
        }
        
    }
    
    /**
     *
     * @param field String
     * @param value String
     * @param datePattern String
     */
    public static void validateDataTypeOfDate(String field, String value,
            String datePattern) {
        
        if (!Util.validateDate(value, datePattern)) {
            throw new VssException(VssConstants.CODE_DATE,
                    new String[] {field});
        }
        
    }
    /**
    *
    * @param field String
    * @param value String
    * @param required boolean
    * @param maxLength int
    */
    public static void validateIpAddress(String field,String value
    		,boolean required){
    	 if(Util.strIsNull(value)){
             if(required){
                 throw new VssException(VssConstants.CODE_REQUIRED,  new String[] {field});
             }else{
                 return;
             }
         }
    	 validateIpAddress(field, value);
    	
    }
    /**
    *
    * @param field String
    * @param value String   
    */
    public static void validateIpAddress(String field,String value){
    	boolean ipPartStatus=false;
    	String[] ipPart=null;
    	ipPart=value.split("\\.");
    	if(ipPart.length!=4){
    		throw new VssException(VssConstants.CODE_IPADDRESS_INVALID,new String[]{field});
    	}    	
    	for(int i=0;i<ipPart.length;i++){
    		int ipPar=Integer.parseInt(ipPart[i]);
    		if(ipPar>=0 && ipPar<=255){
    			ipPartStatus=false;
    			continue;
    		}else{
    			ipPartStatus=true;
    			break;
    		}
    	}
    	if(ipPartStatus){
    		throw new VssException(VssConstants.CODE_IPADDRESS_INVALID,new String[]{field});
    	} 
    	    	
    }
    
}
